package com.isunland.managesystem.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.GuideSecondListAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.DynamicConfigLab;
import com.isunland.managesystem.entity.GuideMenu;
import com.isunland.managesystem.entity.GuideMenuNewOriginal;
import com.isunland.managesystem.entity.SuccessMessage;
import com.isunland.managesystem.utils.SharedPreferencesUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 对象功能:  二级菜单列表
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/3/15 10:07
 */
public class GuideSecondListFragmen extends BaseListFragment {
  protected static final String EXTRA_VALUE =
      "com.isunland.managesystem.ui.GuideSecondListFragmen.EXTRA_VALUE";

  private GuideMenu mMenu;
  private ArrayList<GuideMenu> mList;
  /** 配置了从alisis到activity 的映射 */
  private HashMap<Integer, Class> mMap;

  public static GuideSecondListFragmen newInstance(GuideMenu menu) {

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_VALUE, menu);
    GuideSecondListFragmen fragment = new GuideSecondListFragmen();
    fragment.setArguments(args);
    return fragment;
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mMenu = (GuideMenu) getArguments().getSerializable(EXTRA_VALUE);
    setHasOptionsMenu(true);
    ActionBar actionBar = mActivity.getSupportActionBar();
    if (actionBar != null) {
      actionBar.setTitle(mMenu.getResName());
      actionBar.setDisplayHomeAsUpEnabled(true);
    }
    mMap = initMap();
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  @Override public String getUrl() {
    return ApiConst.URL_GUIDE_SECOND_MENU;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("parentid", mMenu.getResId());
    return params;
  }

  @Override public void resolveResponse(String json) {
    // dialog消失
    GuideMenuNewOriginal original = new Gson().fromJson(json, GuideMenuNewOriginal.class);

    if (original == null) {
      ToastUtil.show("解析数据为空，重新登录！");
      Intent i = new Intent(getActivity(), LoginActivity.class);
      startActivity(i);
    } else {
      String result = original.getResult();
      if (result.equals(SuccessMessage.FAILURE)) {
        ToastUtil.show(original.getMessage());
      } else {
        List<GuideMenu> rows = original.getRows();
        if (mList == null) {
          mList = new ArrayList<>();
        }
        mList.clear();
        mList.addAll(rows);
        GuideSecondListAdapter mAdapter = new GuideSecondListAdapter(getActivity(), mList);
        setListAdapter(mAdapter);
      }
    }
  }

  @Override protected String setEmptyText() {
    return "您的访问权限不够，请确认贵公司是否选购 或 是否授予您此权限。";
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    GuideMenu menu = mList.get(position - 1);
    String flag = menu.getRoleTypeFlag();
    // 更新服务端类别参数.
    BaseVolleyActivity.setRoleTypeFlag(flag);
    int code;
    try {
      code = Integer.parseInt(menu.getAlias());
      startByCode(code);
    } catch (NumberFormatException e) {
      e.printStackTrace();
    }

    super.onListItemClick(l, v, position, id);
  }

  private void startByCode(int code) {
    Class activity = mMap.get(code);
    if (activity == null) {
      ToastUtil.show(R.string.wait_apps);
      return;
    }
    Intent intent = new Intent(mActivity, activity);
    switch (code) {
      case 4:
        intent.putExtra(TaskListFragment.EXTRA_MENU_ID, TaskListFragment.MY_TODO_LIST);
        break;
      case 111:
        //待办
        intent.putExtra(TaskListFragment.EXTRA_MENU_ID, TaskListFragment.MY_TODO_LIST);
        break;
      //case 21:
      //    intent.putExtra(TaskListFragment.EXTRA_MENU_ID, TaskListFragment.MY_TODO_LIST);
      //    break;
      case 22:
        intent.putExtra(TaskListFragment.EXTRA_MENU_ID, TaskListFragment.MY_DONE_LIST);
        break;

      case 42:
        //初始化动态配置.
        DynamicConfigLab.clearMap();//每次进入,清空配置信息,因为配置可能有变化,
        DynamicConfigLab.newInstance(mActivity, getString(R.string.module_deptColumns));
        break;

      case 43:
        DynamicConfigLab.clearMap();//每次进入,清空配置信息,因为配置可能有变化,
        DynamicConfigLab.newInstance(mActivity, getString(R.string.module_subConColumns));
        break;
      case 44:
        DynamicConfigLab.clearMap();//每次进入,清空配置信息,因为配置可能有变化,
        DynamicConfigLab.newInstance(mActivity, getString(R.string.module_developExpenseEecord));
        break;
      case 51:
        DynamicConfigLab.clearMap();//每次进入,清空配置信息,因为配置可能有变化,
        DynamicConfigLab.newInstance(mActivity, getString(R.string.module_appFinance));
        break;
      case 2002:
        //农机服务
        //intent.putExtra(AgricultureServiceFragment.EXTRA_TYPE, "02");
        intent.putExtra(AgricultureServiceFragment.EXTRA_TITLE, getString(R.string.trechForum));
        SharedPreferencesUtil.setString(mActivity, SharedPreferencesUtil.KEY_FORUM_SUBCODE, "02");
        break;
      case 2003:
        //农技服务
        //intent.putExtra(AgricultureServiceFragment.EXTRA_TYPE, "01");
        intent.putExtra(AgricultureServiceFragment.EXTRA_TITLE,
            getString(R.string.AgricultureService));
        SharedPreferencesUtil.setString(mActivity, SharedPreferencesUtil.KEY_FORUM_SUBCODE, "01");
        break;
      case 2006:
        //观光认养
        intent.putExtra(AgricultureGroomListFragment.EXTRA_TYPE,
            AgricultureGroomListFragment.AGRICULTURAL_VISIT);
        break;
      case 2007:
        //农资交易中心
        intent.putExtra(AgricultureGroomListFragment.EXTRA_TYPE,
            AgricultureGroomListFragment.AGRICULTURAL_PRODUCTION);
        break;
    }
    startActivity(intent);
  }

  private HashMap<Integer, Class> initMap() {
    HashMap<Integer, Class> map = new HashMap<>();

    map.put(110, AttendancePagerActivity.class);//考勤打卡
    map.put(112, WorkProcessListActivity.class);//工作任务
    map.put(113, LaTestAnnounceMentActivity.class);//最新消息
    map.put(111, TaskListActivity.class);//待办
    //map.put(5, OperationTaskListActivity.class);
    //map.put(6, AppointmentOrderListActivity.class);

    map.put(11, SignFieldActivity.class);// 外勤签到
    map.put(12, RequisitionListActivity.class);// 考勤申请
    map.put(13, AttendanceSummaryListActivity.class);// 考勤汇总
    map.put(14, MonthAttendanceListActivity.class);// 月考勤数据
    map.put(15, AttendanceSummaryQueryListActivity.class);// 考勤申请单汇总查询

    map.put(21, TaskListActivity.class);//待办
    map.put(22, TaskListActivity.class);//已办
    map.put(23, RequestPagerActivity.class);//我的请求与办结

    map.put(31, WorkProcessListActivity.class);
    map.put(32, DelegateTaskActivity.class);
    map.put(33, PlanApproveActivity.class);
    map.put(34, TaskCollectActivity.class);

    map.put(41, ProductLedgerListActivity.class);
    map.put(42, CustomerListActivity.class);
    map.put(43, ProductSalesListActivity.class);
    map.put(44, ProductDevelopListActivity.class);
    map.put(45, CustomerProblemListActivity.class);//客户问题处理登记
    map.put(46, CustomerAddressBookActivity.class);//客户通讯录
    map.put(115, CustomerNeedListActivity.class);//客户需求与往来

    map.put(51, EmployeeLoanActivity.class);
    map.put(52, DepartmentCollectActivity.class);

    map.put(61, PhoneNumberActivity.class);//企业通信录
    map.put(62, MessagePagerActivity.class);//我的消息
    map.put(63, CompanyForumTypeActivity.class);//企业交流论坛
    map.put(64, KnowledgeAnnounceListActivity.class);//只是公文查阅
    map.put(65, PublishAccnouceListActivity.class);//发布企业公告
    map.put(66, WorkRequestListActivity.class);//日常办公协作
    map.put(67, CarUsedListActivity.class);//车辆使用情况登记
    map.put(68, CompanyEventsListActivity.class);//公司大事记
    map.put(69, PlanManageListActivity.class);//工作日程管理
    map.put(118, CarUsedApplyListActivity.class);//车辆使用申请
    map.put(119, SealUseListActivity.class);//"企业证章使用申请"
    map.put(120, BuyApplyListActivity.class);//采购申请

    map.put(71, ShouldGatherPayListActivity.class);//应收应付查询
    map.put(72, ReceiptListActicity.class);//合同开票

    map.put(81, OderSaleListActivity.class);//销售订单填报
    map.put(82, DeliveryListActicity.class);//配送发货管理
    map.put(85, OrderSaleCollectActivity.class);//出入库汇总查询

    map.put(91, ProjectListActivity.class);//项目进度登记
    map.put(92, ProjectCollectListActivity.class);//项目查询统计
    map.put(114, ProjectLedgerListActivity.class);//项目台账登记

    map.put(101, StaffScoreFillListActivity.class);//奖惩积分填报
    map.put(102, PersonalScoreListActivity.class);//奖惩积分查询
    map.put(104, ScoreRankListActivity.class);//奖惩积分排名

    map.put(110, AttendancePagerActivity.class);//考勤打卡
    map.put(111, TaskListActivity.class);//待办事宜
    map.put(112, WorkProcessListActivity.class);//工作任务
    map.put(113, LaTestAnnounceMentActivity.class);//最新公告

        /*易农菜单*/
    map.put(2001, AppointmentOrderListActivity.class);//呼叫服务
    //        map.put(2002, KnowledgeForumTypeListActivity.class);//农机服务
    map.put(2002, AgricultureServiceActivity.class);//农机服务
    map.put(2003, AgricultureServiceActivity.class);//农技服务
    map.put(2004, KnowledgeBaseListActivity.class);//农业知识库
    map.put(2005, CommonMessageListActivity.class);//公共服务消息
    map.put(2006, AgricultureGroomListActivity.class);//观光农业认养
    map.put(2007, AgricultureGroomListActivity.class);//农资交易中心
    map.put(2008, FinanceFindListActivity.class);//金融保险,更名为发现
    map.put(2009, LeaseOrderListActivity.class);//设备在线租赁
    map.put(2010, CustomerProblemBackActivity.class);//用户问题反馈
    map.put(2011, LocaleWorkQueryCountListActivity.class);//作业完任务登记
    map.put(2012, OnlineReceiveOrderListActivity.class);//接单服务
    map.put(2013, WorkAreaSearchListActivity.class);//地块库
    map.put(2014, PayMoneyLogPagerActivity.class);//支付中心

    return map;
  }
}
